package cn.exrick.xboot.modules.research.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;

import cn.exrick.xboot.modules.research.entity.DocumentManagement;
import cn.exrick.xboot.modules.research.service.IDocumentManagementService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lty
 */
@Slf4j
@RestController
@Api(tags = "科研论文管理管理接口")
@RequestMapping("/xboot/documentManagement")
@Transactional
public class DocumentManagementController {

    @Autowired
    private IDocumentManagementService iDocumentManagementService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<DocumentManagement> get(@PathVariable String id) {

        DocumentManagement documentManagement = iDocumentManagementService.getById(id);
        return new ResultUtil<DocumentManagement>().setData(documentManagement);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<DocumentManagement>> getAll() {

        List<DocumentManagement> list = iDocumentManagementService.list();
        return new ResultUtil<List<DocumentManagement>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<DocumentManagement>> getByPage(@ModelAttribute DocumentManagement documentManagement,@ModelAttribute PageVo page) {
        QueryWrapper<DocumentManagement> qw =new QueryWrapper<>();

        if (StrUtil.isNotBlank(documentManagement.getDocumentId())){
             qw.like("document_Id",documentManagement.getDocumentId());
        }

        if (StrUtil.isNotBlank(documentManagement.getDocumentName())){
            qw.like("document_Name",documentManagement.getDocumentName());
        }

        if (StrUtil.isNotBlank(documentManagement.getAuthor())){
            qw.like("author",documentManagement.getAuthor());
        }

        if (StrUtil.isNotBlank(documentManagement.getTime())){
            qw.eq("time",documentManagement.getTime());
        }

        IPage<DocumentManagement> data = iDocumentManagementService.page(PageUtil.initMpPage(page),qw);
        return new ResultUtil<IPage<DocumentManagement>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<DocumentManagement> saveOrUpdate(DocumentManagement documentManagement) {

        if (iDocumentManagementService.saveOrUpdate(documentManagement)) {
            return new ResultUtil<DocumentManagement>().setData(documentManagement);
        }
        return new ResultUtil<DocumentManagement>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@RequestParam String[] ids) {

        for (String id : ids) {
            iDocumentManagementService.removeById(id);
        }
        return ResultUtil.success("批量通过id删除数据成功");
    }
}
